<?php

class User extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function authorize() {
        $params = $this->input->post();
        if (empty($params) == FALSE) {
            if(empty($params['access_token']) == TRUE){
                $data = parse_url($params['data']);
                parse_str($data['fragment'], $result);
            }else{
                $result['access_token'] = $params['access_token'];
            }

            $graph = 'https://graph.facebook.com/v2.1/me/?access_token=' . $result['access_token'];
            $json = file_get_contents($graph);
            $data = json_decode($json, TRUE);

            $user_info['access_token'] = $params['access_token'];
            $user_info['facebook_id'] = $data['id'];
            $_SESSION['user_info'] = $user_info;            
        }
    }

    public function logout() {
        $this->load->helper('url');
        session_destroy();        
        redirect(base_url());
    }

}
